/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.activemq;

import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.core.env.Environment;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * activemq配置工厂
 *
 * @author yoara
 */
public class ConfigFactory {
    public static final String BASIC_ACTIVEMQ_URL = "basic.activemq.url";       //activemq地址
    public static final String BASIC_ACTIVEMQ_SESSION_SIZE = "basic.activemq.sessionSize";  //缓存池尺寸

    public static ActiveMQConnectionFactory makeActiveMQConnectionFactory(
            Environment environment,String url) {
        ActiveMQConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory();
        amqConnectionFactory.setBrokerURL(environment.getProperty(url));
        amqConnectionFactory.setSendTimeout(10000);
        amqConnectionFactory.setConnectResponseTimeout(5000);
        amqConnectionFactory.setTrustAllPackages(true);
        return amqConnectionFactory;
    }

    public static CachingConnectionFactory makeCachingConnectionFactory(
            Environment environment,ActiveMQConnectionFactory amqConnectionFactory,String sessionSize) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(amqConnectionFactory);
        connectionFactory.setSessionCacheSize(
                environment.getProperty(sessionSize,Integer.class));
        return connectionFactory;
    }

    public static JmsTemplate makeJmsTemplate(CachingConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        return jmsTemplate;
    }
}
